<?php
class PostsService{

	public  $dbutil;
	function __construct($dbutil){
		$this->dbutil =  $dbutil;
	}
	/**
	 *
	 * 总结果集
	 */
	public function postsPage($start , $page_size ,$condition){
		$limit ="";
		if($page_size){
			$limit =" limit $start,$page_size ";
		}
		if(empty($condition)){
			$sql = "SELECT ID, post_title,  post_author, post_category, comment_count, post_date,  post_status, category_name, tags FROM posts, post_category WHERE posts.post_category=category_id".$condition." order by ID desc $limit";
		}else{
			$sql = "SELECT ID, post_title,  post_author, post_category, comment_count, post_date,  post_status, category_name, tags FROM posts, post_category WHERE posts.post_category=category_id AND ".$condition." order by ID desc $limit";
		}
		return $this->dbutil->get_results($sql);
	}
	/**
	 *
	 * 总记录数
	 */
	public function countNum($condition) {
		if(empty($condition)){
			$sql = "SELECT count(1) countnum FROM posts, post_category WHERE posts.post_category=category_id".$condition;
		}else{
			$sql = "SELECT count(1) countnum FROM posts, post_category WHERE posts.post_category=category_id AND ".$condition;
		}
		$resutrnarry = $this->dbutil->get_results ( $sql );
		return $resutrnarry [0]->countnum;
	}
	
	public function removePosts($id) {
		$sql = " delete from posts where id=$id";//删除子节点
		return $this->dbutil->query($sql);
	}
	
	public function getAllPosts(){
		$sql = "select * from posts ";
		return $this->dbutil->get_results($sql);
	}
	
	public function getPostById($postId){
		$sql = "select * from posts where ID = $postId";
		return $this->dbuti->get_row();
	}
}